<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '格知汇GACC',
    navigationStyle: 'custom',
  },
}
</route>
<template>
  <view class="entire">
    <image
      style="z-index: -10; position: fixed; top: 0; width: 100vw; height: 100vh"
      :src="listBg"
      mode="aspectFill"
    />
    <view class="div1">
      <Backbar title="公告" />
      <!-- 一级选择器 -->
      <wht-tabs
        v-model="pageData.tab1"
        :list="pageData.tabList1"
        @change="handleTab1"
        bgColor="#e5effd"
        color="#898889"
        active-color="#254872"
        line-color="#254872"
        :font-size="26"
        :isRound="true"
        :min-width="175"
        :padding="100"
      />
    </view>
    <scroll-view
      ref="scrollContainer"
      scroll-y="true"
      class="div2"
      enable-flex="true"
      :style="{ height: pageData.div2Height + 'px' }"
    >
      <!-- 空显示 -->
      <view
        v-if="
          (pageData.tab1 === 0 && pageData.msgList1.length === 0) ||
          (pageData.tab1 === 1 && pageData.msgList2.length === 0)
        "
        style="
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
        "
      >
        <image src="/static/images/icons/imgNone.png" style="width: 309rpx; height: 259rpx" />
      </view>
      <!-- 未读消息列表 -->
      <view v-if="pageData.tab1 === 0">
        <view v-for="(item, index) in pageData.msgList1" :key="index">
          <NoticeItem :msg="item" />
        </view>
      </view>
      <!-- 已读消息列表 -->
      <view v-if="pageData.tab1 === 1">
        <view v-for="(item, index) in pageData.msgList2" :key="index">
          <NoticeItem :msg="item" />
        </view>
      </view>
      <!--加载中，到底啦横幅-->
      <view
        style="margin-top: 30rpx; padding-bottom: 50rpx"
        v-if="
          !(
            (pageData.tab1 === 0 && pageData.msgList1.length === 0) ||
            (pageData.tab1 === 1 && pageData.msgList2.length === 0)
          )
        "
      >
        <wd-divider color="black">{{ pageData.loading ? '加载中...' : '已经到底了' }}</wd-divider>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { getNoticeList } from '@/service/leftTop/leftTop'
import NoticeItem from '@/components/MessegeItem/NoticeItem.vue'
import { listBg } from '@/static/imgMap'

const pageData = reactive({
  tab1: 0,
  tabList1: ['未读', '已读'],
  div2Height: 0,
  // 未读
  msgList1: [],
  // 已读
  msgList2: [],
  loading: false,
})
const handleTab1 = (index) => {
  console.log(index)
}

const fetchNotices = async () => {
  try {
    const unreadNotices = await getNoticeList(0)
    const readNotices = await getNoticeList(1)
    pageData.msgList1 = unreadNotices.data
    pageData.msgList2 = readNotices.data
  } catch (error) {
    console.error('Failed to fetch notices:', error)
  }
}
// 判断列表为空显示
onMounted(() => {
  // 自适应滑动列表长度
  uni.getSystemInfo({
    success(res) {
      const screenHeight = res.windowHeight
      console.log(screenHeight)
      uni
        .createSelectorQuery()
        .select('.div1')
        .boundingClientRect((data) => {
          pageData.div2Height = screenHeight - data.height
        })
        .exec()
    },
  })
  fetchNotices()
})
</script>

<style scoped lang="scss">
.entire {
  width: 100%;
  .div1 {
    background-color: rgb(37, 73, 115);
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgb(37, 73, 115);
      width: 100%;
      height: 120rpx;
      color: #fff;
      font-size: large;
    }
  }
  .div2 {
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow-y: scroll;
  }
}
</style>
